package com.bs.visitservice.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.cache.impl.JedisLoginCache;
import com.bs.visitservice.entity.ReceiverAddress;
import com.bs.visitservice.service.ReceiverAddressService;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import com.bs.visitservice.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户地址信息管理
 */
@RestController
@RequestMapping("/user")
public class ReceiverAddressController {

    @Autowired
    private JedisLoginCache jedisCacheManager;
    @Autowired
    private ReceiverAddressService receiverAddressService;

    /**
     * 查询用户地址信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "/queryAddress",method = RequestMethod.POST)
    public Result queryAddress(@RequestBody Map cond){
        Long userNo = (Long) ThreadLocalUtil.get("loginNo");
        IPage page = ParamUtils.getPage(cond);
        LambdaQueryWrapper<ReceiverAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReceiverAddress::getUserNo, userNo);
        wrapper.eq(ReceiverAddress::getIsValid,"1");
        page = receiverAddressService.pageMaps(page,wrapper);
        List<Map<String, Object>> res = page.getRecords();
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("datas",res);
        return Result.success(result);
    }
    /**
     * 增加用户地址信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "/saveAddress",method = RequestMethod.POST)
    public Result saveAddress(@RequestBody Map cond){
        ReceiverAddress ra =  (ReceiverAddress)ResultUtil.transObject(cond,ReceiverAddress.class);
        Long userNo = (Long)ThreadLocalUtil.get("loginNo");
        ra.setUserNo(userNo);
        ra.setIsValid("1");
        boolean isSuc = receiverAddressService.save(ra);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }
    /**
     * 更新用户地址信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    public Result updateAddress(@RequestBody Map cond){
        ReceiverAddress ra =  (ReceiverAddress)ResultUtil.transObject(cond,ReceiverAddress.class);
        LambdaQueryWrapper<ReceiverAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReceiverAddress::getNo,ra.getNo());
        boolean isSuc = receiverAddressService.update(ra,wrapper);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }

    /**
     * 删除用户地址信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "/removeAddress",method = RequestMethod.POST)
    public Result removeAddress(@RequestBody Map cond){
        ReceiverAddress ra = (ReceiverAddress)ResultUtil.transObject(cond,ReceiverAddress.class);
        Long userNo = (Long)ThreadLocalUtil.get("loginNo");
        List<ReceiverAddress> list = receiverAddressService.lambdaQuery().eq(ReceiverAddress::getUserNo, userNo).eq(ReceiverAddress::getNo, ra.getNo()).list();
        if(list.size()==0){
            return Result.error("地址信息不存在!");
        }
        ra.setIsValid("0");
        boolean isSuc = receiverAddressService.updateById(ra);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }
}
